package com.qf.wangqing.sportsnews.adapter.headline;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.qf.wangqing.sportsnews.R;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by ShenHao on 2016/12/20.
 */

public class AdapterGridView extends RecyclerView.Adapter<AdapterGridView.GridViewHolder> implements View.OnClickListener {
    private List<String> list;
    private Context context;
    private LayoutInflater inflater;
    private RecyclerView recyclerView;

    public AdapterGridView(List<String> list, Context context) {
        this.list = list;
        this.inflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public GridViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        return new GridViewHolder(layoutInflater.inflate(R.layout.danymic_gridview_item, null, false));
    }

    @Override
    public void onBindViewHolder(GridViewHolder holder, int position) {
        holder.imageView.setOnClickListener(this);
        Picasso.with(context).load("http://lifeixphoto.qiniudn.com/" + list.get(position)).into(holder.imageView);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
        GridLayoutManager manager = new GridLayoutManager(context, 3);
        this.recyclerView.setLayoutManager(manager);

        RecyclerView.ItemDecoration decoration = new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                int position = parent.getChildAdapterPosition(view);
                if (position % 3 == 0 || position == 0) {
                    outRect.set(0, 0, 2, 4);
                } else if ((position + 1) % 3 == 0) {
                    outRect.set(2, 0, 0, 4);
                } else {
                    outRect.set(2, 0, 2, 4);
                }
            }
        };
        this.recyclerView.addItemDecoration(decoration);
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }

    @Override
    public void onClick(View view) {

    }

    public static class GridViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;

        public GridViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.image_gride);

        }
    }
}

